package com.ms.oschina.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseFragment;
import com.ms.oschina.ui.MainTab;
import com.ms.oschina.ui.activity.ShotActivity;
import com.ms.oschina.ui.dialog.QuickOptionDialog;
import com.ms.oschina.util.DialogUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.BadgeView;
import com.ms.oschina.view.MyFragmentTabHost;

public class ContentFragment extends BaseFragment implements OnClickListener {
	private ImageView tweet;
	private MyFragmentTabHost mTabHost;
	private FragmentActivity activity;

	private QuickOptionDialog dialog;
	private BadgeView badgeView;

	@Override
	public int getLayoutId() {
		return R.layout.fragment_content;
	}

	@Override
	public void initView(View view) {
		mTabHost = (MyFragmentTabHost) view.findViewById(android.R.id.tabhost);
		tweet = (ImageView) view.findViewById(R.id.iv_mian_tweet);
	}

	@Override
	protected void initData() {
		activity = (FragmentActivity) getActivity();
		initTabs();
	}

	private void initTabs() {
		mTabHost.setup(activity, activity.getSupportFragmentManager(),
				R.id.realtabcontent);
		MainTab[] tabs = MainTab.values();
		for (int i = 0; i < tabs.length; i++) {
			MainTab tab = tabs[i];

			TabSpec newTabSpec = mTabHost.newTabSpec(tab.getIdx() + "");
			View view = View.inflate(activity, R.layout.tab, null);
			if (i == 2) {
				view.setVisibility(View.INVISIBLE);
			} else {
				TextView tv = (TextView) view.findViewById(R.id.tab_title);
				tv.setText(UiUtils.getString(tab.getResName()));
				Drawable drawable = activity.getResources().getDrawable(
						tab.getResIcon());
				// setCompoundDrawablesWithIntrinsicBounds图标的宽高将会设置为固有宽高，
				tv.setCompoundDrawablesWithIntrinsicBounds(null, drawable,
						null, null);
			}

			if (tab == MainTab.ME) {
				TextView tvRedRemind = (TextView) view
						.findViewById(R.id.tab_redRemind);
				badgeView = new BadgeView(getActivity(), tvRedRemind);
				badgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
				badgeView.setGravity(Gravity.CENTER);
				badgeView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
				badgeView.setBackgroundResource(R.drawable.notification_bg);
				badgeView.setText(1 + "");
				badgeView.show();
			}

			newTabSpec.setIndicator(view);

			TabWidget tabWidget = mTabHost.getTabWidget();
			tabWidget.setDividerDrawable(null);
			mTabHost.addTab(newTabSpec, tab.getClz(), null);
		}
	}

	@Override
	public void initEvent() {
		tweet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog = DialogUtils.showSeedTweetDialog(activity,
						getSeedTweetView());
			}

			private View getSeedTweetView() {
				View seedTweetview = UiUtils
						.inflate(R.layout.dialog_seed_tweet);
				LinearLayout contentView = (LinearLayout) seedTweetview
						.findViewById(R.id.ll_seedtweet_content);
				TextView tvSeedtweetText = (TextView) seedTweetview
						.findViewById(R.id.tv_seedtweet_text);
				TextView ivSeedtweetPhoto = (TextView) seedTweetview
						.findViewById(R.id.iv_seedtweet_photo);
				TextView ivSeedtweetTake = (TextView) seedTweetview
						.findViewById(R.id.iv_seedtweet_take);
				ImageView ivSeedtweetCencel = (ImageView) seedTweetview
						.findViewById(R.id.iv_seedtweet_cencel);
				Animation loadAnimation = AnimationUtils.loadAnimation(
						activity, R.anim.quick_option_close);
				loadAnimation
						.setInterpolator(new AccelerateDecelerateInterpolator());
				ivSeedtweetCencel.startAnimation(loadAnimation);
				// 设置点击事件
				contentView.setOnClickListener(ContentFragment.this);
				tvSeedtweetText.setOnClickListener(ContentFragment.this);
				ivSeedtweetPhoto.setOnClickListener(ContentFragment.this);
				ivSeedtweetTake.setOnClickListener(ContentFragment.this);
				ivSeedtweetCencel.setOnClickListener(ContentFragment.this);
				return seedTweetview;
			}
		});

		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals(""+MainTab.ME.getIdx())) {
					badgeView.hide();
				}
			}
		});
	}

	@Override
	public void onResume() {
		if (dialog != null) {
			dialog.dismiss();
		}
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_seedtweet_content:
			dialog.dismiss();
			return;
		case R.id.iv_seedtweet_cencel:
			dialog.dismiss();
			return;
		case R.id.tv_seedtweet_text:
			break;
		case R.id.iv_seedtweet_photo:
			break;
		case R.id.iv_seedtweet_take:
			break;
		}

		Context context = UiUtils.getContext();
		Intent intent = new Intent(context, ShotActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

}
